<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
<link href="/resource/css/bootstrap.css" rel="stylesheet">
<script type="text/javascript" src="/resource/js/jquery-3.2.1.js"></script>
<script type="text/javascript" src="/resource/js/bootstrap.js"></script>
<script type="text/javascript">

function myopen(id){
	window.open("/get?id="+id,"_blank");
}


//全选 反选 全不选
function chk(flag){
	if(flag==1){//全选
		$("[name='ids']").prop("checked",true);
	}else if (flag==2){//反选
		$("[name='ids']").each(function(){
			this.checked=!this.checked;
		})
		
	}else{//全不选
		$("[name='ids']").prop("checked",false);
	}
	
	
	
	
}

//批量删除
function deleteBatch(){
	//获取到所有的选中的checkbox的value 的id
	var ids =new Array();
	$("[name='ids']:checked").each(function(i){
		ids[i]=$(this).val();//遍历选中的值放入ids数组
	})
	if(ids.length==0){
		alert("请至少选中一个");
		return;
	}
	
	if(confirm("确定删除吗？")){
	   $.post("/deleteBatch",{ids:ids},function(flag){
		   if(flag){
			   alert("删除成功");
				location.href="/list";
		   }else{
			   alert("删除失败");
		   }
		   
	   })	
		
		
		
	}
	
	
	
	
	
}
</script>
</head>
<body>
 <div>
  <form action="/list" method="post">
  <input type="hidden" name="pageNum" id="pageNum">
   名称：<input type="text" name="name" value="${plant.name }"><button type="submit" >查询</button> 
 
 </form>
   <a href="/add">添加</a>
   <button type="button" onclick="deleteBatch()">批量删除</button>
 </div>

	<table class="table">
		<tr> 
		    <td>
		    <button type="button" onclick="chk(1)">全选</button>
		    <button type="button" onclick="chk(2)">反选</button>
		    <button type="button" onclick="chk(3)">全不选</button>
		    
		    </td>
			<td>序号</td>
			<td>植物名称</td>
			<td>植物描述</td>
			<td>分类名称</td>
			<td>创建日期</td>
			<td>操作</td>
		</tr>

		<c:forEach items="${info.list}" var="plant">
			<tr>
				<td><input type="checkbox" name="ids" value="${plant.id }"> </td>
				<td>${plant.id }</td>
				<td>${plant.name }</td>
				<td>${plant.mx }</td>
				<td>${plant.cname }</td>
				<td><fmt:formatDate value="${plant.created }" pattern="yyyy-MM-dd"/></td>
				<td><a href="javascript:myopen(${plant.id})">查看</a>| <a href="/update?id=${plant.id}">修改</a>|删除</td>
			</tr>

		</c:forEach>

	</table>
	
	 <jsp:include page="/resource/page/pages.jsp"/>
   
</body>
<script type="text/javascript">

function goPage(pageNum){
	$("#pageNum").val(pageNum);
	$("form").submit();
 //	location.href="/list?pageNum="+pageNum
}

</script>
</html>